<template>
  <div class="home">
    <div class="container">
      <div class="card">
        <h2>RTSP流配置</h2>
        
        <form @submit.prevent="updateConfig" class="config-form">
          <div class="form-group">
            <label for="rtspUrl">RTSP URL:</label>
            <input
              id="rtspUrl"
              v-model="form.rtspUrl"
              type="text"
              placeholder="rtsp://username:password@ip:port/stream"
              required
            />
          </div>
          
          <div class="form-row">
            <div class="form-group">
              <label for="segmentDuration">分段时间 (秒):</label>
              <input
                id="segmentDuration"
                v-model.number="form.segmentDuration"
                type="number"
                min="1"
                max="10"
                required
              />
            </div>
            
            <div class="form-group">
              <label for="segmentMaxFiles">最大分段文件数:</label>
              <input
                id="segmentMaxFiles"
                v-model.number="form.segmentMaxFiles"
                type="number"
                min="5"
                max="20"
                required
              />
            </div>
          </div>
          
          <button type="submit" :disabled="videoStore.loading" class="btn btn-primary">
            {{ videoStore.loading ? '更新中...' : '更新配置' }}
          </button>
        </form>
      </div>
      
      <div class="card">
        <h2>流媒体控制</h2>
        
        <div class="status-display">
          <div class="status-item">
            <span class="label">状态:</span>
            <span :class="['status', videoStore.status]">
              {{ getStatusText(videoStore.status) }}
            </span>
          </div>
          
          <div class="status-item">
            <span class="label">RTSP URL:</span>
            <span class="value">{{ videoStore.rtspUrl }}</span>
          </div>
        </div>
        
        <div class="control-buttons">
          <button
            @click="startStream"
            :disabled="videoStore.loading || videoStore.isRunning"
            class="btn btn-success"
          >
            {{ videoStore.loading ? '启动中...' : '启动流' }}
          </button>
          
          <button
            @click="stopStream"
            :disabled="videoStore.loading || videoStore.isStopped"
            class="btn btn-danger"
          >
            {{ videoStore.loading ? '停止中...' : '停止流' }}
          </button>
        </div>
        
        <div v-if="videoStore.isRunning" class="player-link">
          <router-link to="/player" class="btn btn-info">
            观看视频流
          </router-link>
        </div>
      </div>
      
      <div v-if="videoStore.hasError" class="error-card">
        <h3>错误信息</h3>
        <p>{{ videoStore.error }}</p>
        <button @click="videoStore.clearError" class="btn btn-secondary">
          清除错误
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useVideoStore } from '../stores/video'

const videoStore = useVideoStore()

const form = reactive({
  rtspUrl: videoStore.rtspUrl,
  segmentDuration: videoStore.segmentDuration,
  segmentMaxFiles: videoStore.segmentMaxFiles
})

const getStatusText = (status) => {
  const statusMap = {
    running: '运行中',
    stopped: '已停止',
    error: '错误'
  }
  return statusMap[status] || status
}

const updateConfig = async () => {
  await videoStore.updateConfig(form)
}

const startStream = async () => {
  await videoStore.startStream()
}

const stopStream = async () => {
  await videoStore.stopStream()
}

onMounted(() => {
  videoStore.fetchStatus()
})
</script>

<style scoped>
.home {
  width: 100%;
}

.container {
  display: grid;
  gap: 2rem;
}

.card {
  background: white;
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  border: 1px solid #e9ecef;
}

.card h2 {
  margin: 0 0 1.5rem 0;
  color: #333;
  font-size: 1.5rem;
}

.config-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

label {
  font-weight: 500;
  color: #555;
}

input {
  padding: 0.75rem;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.2s;
}

input:focus {
  outline: none;
  border-color: #667eea;
}

.control-buttons {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
}

.status-display {
  margin-bottom: 1.5rem;
}

.status-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 0;
  border-bottom: 1px solid #f1f3f4;
}

.status-item:last-child {
  border-bottom: none;
}

.label {
  font-weight: 500;
  color: #555;
}

.value {
  color: #666;
  word-break: break-all;
}

.status {
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.875rem;
  font-weight: 500;
}

.status.running {
  background: #d4edda;
  color: #155724;
}

.status.stopped {
  background: #f8d7da;
  color: #721c24;
}

.status.error {
  background: #f8d7da;
  color: #721c24;
}

.player-link {
  margin-top: 1.5rem;
  text-align: center;
}

.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  text-decoration: none;
  display: inline-block;
  text-align: center;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: #667eea;
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: #5a6fd8;
}

.btn-success {
  background: #28a745;
  color: white;
}

.btn-success:hover:not(:disabled) {
  background: #218838;
}

.btn-danger {
  background: #dc3545;
  color: white;
}

.btn-danger:hover:not(:disabled) {
  background: #c82333;
}

.btn-info {
  background: #17a2b8;
  color: white;
}

.btn-info:hover:not(:disabled) {
  background: #138496;
}

.btn-secondary {
  background: #6c757d;
  color: white;
}

.btn-secondary:hover:not(:disabled) {
  background: #5a6268;
}

.error-card {
  background: #f8d7da;
  border: 1px solid #f5c6cb;
  border-radius: 12px;
  padding: 1.5rem;
  color: #721c24;
}

.error-card h3 {
  margin: 0 0 1rem 0;
  color: #721c24;
}

.error-card p {
  margin: 0 0 1rem 0;
}

@media (max-width: 768px) {
  .form-row {
    grid-template-columns: 1fr;
  }
  
  .control-buttons {
    flex-direction: column;
  }
}
</style>
